package dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import model.User;

import org.hibernate.Session;

import controller.HibernateController;

public class UserDAO implements IUserDAO
{

    public void adduser(User user) throws SQLException
    {
        Session session = null;
        try
        {
            session = HibernateController.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage(), "I/O Error", JOptionPane.OK_OPTION);
        }
        finally
        {
            if (session != null && session.isOpen())
            {
                session.close();
            }
        }
    }

    public void updateUser(User user) throws SQLException
    {
        Session session = null;
        try
        {
            session = HibernateController.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(user);
            session.getTransaction().commit();
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.OK_OPTION);
        }
        finally
        {
            if (session != null && session.isOpen())
            {
                session.close();
            }
        }
    }

    public User getUserById(int id) throws SQLException
    {
        Session session = null;
        User stud = null;
        try
        {
            session = HibernateController.getSessionFactory().openSession();
            stud = (User) session.load(User.class, id);
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage(), " I/O", JOptionPane.OK_OPTION);
        }
        finally
        {
            if (session != null && session.isOpen())
            {
                session.close();
            }
        }
        return stud;
    }

    public List<User> getAllUsers() throws SQLException
    {
        Session session = null;
        List<User> studs = new ArrayList<User>();
        try
        {
            session = HibernateController.getSessionFactory().openSession();
            studs = session.createCriteria(User.class).list();
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage(), " I/O", JOptionPane.OK_OPTION);
        }
        finally
        {
            if (session != null && session.isOpen())
            {
                session.close();
            }
        }
        return studs;
    }

    public void deleteUser(User user) throws SQLException
    {
        Session session = null;
        try
        {
            session = HibernateController.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(user);
            session.getTransaction().commit();
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage(), "I/O", JOptionPane.OK_OPTION);
        }
        finally
        {
            if (session != null && session.isOpen())
            {
                session.close();
            }
        }
    }

}
